package com.ssmdemo.controller;

import com.ssmdemo.aop.bean.PermissionCode;
import com.ssmdemo.pojo.MenuEntity;
import com.ssmdemo.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/getMenu")
public class GetMenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping("/toMenuList")
    @PermissionCode(value = "role:page")
    public String toMenuList(){
        return "menu/menuList";
    }

    @RequestMapping("/toUpdateMenu/{id}")
    public String toAddMenu(@PathVariable Integer id, Model model){
        MenuEntity menuEntity=menuService.selectById(id);
        menuEntity.setParentMenuEntity(menuService.selectById(menuEntity.getParentId()));
        model.addAttribute("menu",menuEntity);
        return "menu/updateMenu";
    }

    @RequestMapping("/toAddMenu")
    public String toAddMenuZtree(){
        return "menu/addMenu";
    }

//    @RequestMapping("/toAddMenuZtree")
//    public String toAddMenuZtree(){
//        return "menu/menuZtree";
//    }

    @RequestMapping("/getMenuList")
    public String getMenuList(Model model){
        List<MenuEntity> menuEntityList=menuService.selectList(null);
        model.addAttribute("menuList",menuEntityList);
        for (MenuEntity menuEntity:menuEntityList){
            System.out.println(menuEntity.getMenuTypeString());
        }

        return "menu/menuList";
//        return "redirect:toMenuList";
    }

    @RequestMapping("/getAddMenu")
    public String getAddMenu(MenuEntity menuEntity){
//        boolean i=menuService.insert(menuEntity);
        menuService.insertMenu(menuEntity);
//        return "menu/menuList";
        return "redirect:getMenuList";
    }


    @RequestMapping("/getUpdateMenu")
    public String getUpdateMenu(MenuEntity menuEntity){

        menuService.updateMenu(menuEntity);
        return "redirect:getMenuList";
    }

    @RequestMapping("/getMenuListByParentId")
    @ResponseBody
    public List<Map<String,Object>> getMenuListByParentId(Integer id){

        if (id==null){
            id=0;
        }

        List<MenuEntity> menuEntityList=menuService.getMenuListByParentId(id);

        List<Map<String,Object>> mapList=new ArrayList<Map<String, Object>>();

        for (MenuEntity menuEntity:menuEntityList){
            Map<String,Object> map=new HashMap<String, Object>();
            map.put("id",menuEntity.getId());
            map.put("name",menuEntity.getMenuName());
            map.put("isParent",menuEntity.getSubId() != null ?true:false);
            mapList.add(map);

        }


        return mapList;
//        return "redirect:toMenuList";
    }
}
